package com.farm.kaiping.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.farm.common.constant.CommonConstants;
import com.farm.common.security.SecurityUtils;
import com.farm.kaiping.entity.SysOauthClientDetails;
import com.farm.kaiping.po.SysOauthClientDetailsPO;
import com.farm.kaiping.mapper.SysOauthClientDetailsMapper;
import com.farm.kaiping.service.SysOauthClientDetailsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.farm.common.util.StringRange;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 终端信息表(SysOauthClientDetails)表服务实现类
 *
 * @author zhuhui
 * @since 2024-11-27 21:01:26
 */
@Service
@Slf4j
@AllArgsConstructor
public class SysOauthClientDetailsServiceImpl extends ServiceImpl<SysOauthClientDetailsMapper, SysOauthClientDetails> implements SysOauthClientDetailsService {

	private final StringRedisTemplate stringRedisTemplate;

    
	private LambdaQueryWrapper<SysOauthClientDetails> generateQueryWrapper(SysOauthClientDetailsPO sysOauthClientDetailsPO){
		LambdaQueryWrapper<SysOauthClientDetails> queryWrapper = new LambdaQueryWrapper<>();
		// ID
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysOauthClientDetailsPO.getId()), SysOauthClientDetails::getId, sysOauthClientDetailsPO.getId());
		queryWrapper.in(ObjectUtil.isNotEmpty(sysOauthClientDetailsPO.getIdList()), SysOauthClientDetails::getId, sysOauthClientDetailsPO.getIdList());
		// 客户端ID
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysOauthClientDetailsPO.getClientId()), SysOauthClientDetails::getClientId, sysOauthClientDetailsPO.getClientId());
		// 客户端秘钥
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysOauthClientDetailsPO.getClientSecret()), SysOauthClientDetails::getClientSecret, sysOauthClientDetailsPO.getClientSecret());
		// 删除标记，0未删除，1已删除
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysOauthClientDetailsPO.getDelFlag()), SysOauthClientDetails::getDelFlag, sysOauthClientDetailsPO.getDelFlag());
        queryWrapper.eq(ObjectUtil.isEmpty(sysOauthClientDetailsPO.getDelFlag()), SysOauthClientDetails::getDelFlag, 0);
		return queryWrapper;
	}
	
	/**
     * @describe 查询终端信息表list
     * @author  zhuhui
     * @date  2024-11-27 21:01:26
     */
	@Override
	public List<SysOauthClientDetails> getSysOauthClientDetailsList(SysOauthClientDetailsPO sysOauthClientDetailsPO){
		LambdaQueryWrapper<SysOauthClientDetails> queryWrapper = this.generateQueryWrapper(sysOauthClientDetailsPO);
        return this.baseMapper.selectList(queryWrapper);
	}
	
	/**
     * @describe 获取一条数据 终端信息表
     * @author  zhuhui
     * @date  2024-11-27 21:01:26
     */
	@Override
	public SysOauthClientDetails getSysOauthClientDetails(SysOauthClientDetailsPO sysOauthClientDetailsPO){
		LambdaQueryWrapper<SysOauthClientDetails> queryWrapper = this.generateQueryWrapper(sysOauthClientDetailsPO);
        queryWrapper.last("LIMIT 1");
        return this.baseMapper.selectOne(queryWrapper);
	}

	@Override
	public Boolean verifyClient(String clientId, String clientSecret) {
		if(!ObjectUtil.isAllNotEmpty(clientId,clientSecret)){
			return false;
		}
		String s1 = stringRedisTemplate.opsForValue().get(CommonConstants.OAUTH_CLIENT + clientId);
        return ObjectUtil.isNotEmpty(s1) && ObjectUtil.equal(clientSecret, s1);
    }

}

